package com.wizz.trade.information.controller;

import com.wizz.trade.information.dto.BankInfoDto;
import com.wizz.trade.information.dto.page.BankInfoPageDto;
import com.wizz.trade.information.entity.Bankinfo;
import com.wizz.trade.information.service.BankinfoService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.vo.BankInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 资料模块/开户银行
 */
@RestController
@RequestMapping("/bankinfo")
@Slf4j
public class BankinfoController {

    @Resource
    private BankinfoService bankinfoService;

    /**
     * 添加操作
     *
     * @param bankInfoDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult insert(@RequestBody BankInfoDto bankInfoDto) {
        Bankinfo bankinfo = new Bankinfo();
        BeanUtils.copyProperties(bankInfoDto, bankinfo);
        bankinfoService.addBankinfo(bankinfo);
        return new ResponseResult<>(StatusCode.OK, "新增成功");
    }

    /**
     * 编辑操作
     *
     * @param bankInfoDto
     * @param id
     * @return
     */

    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult update(@RequestBody BankInfoDto bankInfoDto, @PathVariable("id") Integer id) {
        Bankinfo bankinfo = new Bankinfo();
        BeanUtils.copyProperties(bankInfoDto, bankinfo);
        bankinfo.setId(id);
        bankinfoService.updateBankinfo(bankinfo);
        return new ResponseResult(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销操作）
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        bankinfoService.deleteBankinfo(id);
        return new ResponseResult(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     *
     * @param bankInfoPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult<PageVo<BankInfoVo>> list(BankInfoPageDto bankInfoPageDto) {
        PageVo<Bankinfo> rawPage = bankinfoService.getPage(bankInfoPageDto);
        List<Bankinfo> data = rawPage.getData();
        List<BankInfoVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            BankInfoVo bankInfoVo = new BankInfoVo();
            BeanUtils.copyProperties(data.get(i), bankInfoVo);
            dataVo.add(bankInfoVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取所有银行信息
     *
     * @return
     */
    @GetMapping("/all")
    public ResponseResult<List<Bankinfo>> getAllBankinfo() {
        List<Bankinfo> bankinfos = bankinfoService.getAll();
        return new ResponseResult<>(StatusCode.OK, bankinfos);
    }

}
